package org.redis.cluster.interceptor.support;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.Callable;

import org.redis.cluster.interceptor.Interceptor;
import org.redis.cluster.interceptor.Invocation;
import org.redis.cluster.redis.RedisCommand;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.transport.session.Session;

public class DefaultInvocation implements Invocation {
	
	private Session session;
	private RedisCommand command;
	private Iterator<Interceptor> interceptors;
	private Callable<RedisReply> callable;
	
	public DefaultInvocation(Session session,RedisCommand command,Collection<Interceptor> interceptors,Callable<RedisReply> callable) {
		this.session=session;
		this.command=command;
		this.interceptors=interceptors.iterator();
		this.callable=callable;
	}

	@Override
	public Session getSession() {
		return session;
	}
	
	@Override
	public RedisCommand getCommand() {
		return command;
	}

	@Override
	public RedisReply invoke() throws Throwable {
		if(interceptors.hasNext()) {
			return interceptors.next().intercept(this);
		}
		
		return callable.call();
	}

}